<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="Mark Otto, Jacob Thornton, and Bootstrap contributors">
    <meta name="generator" content="Jekyll v4.0.1">
    <title>注册-Management</title>

    <link rel="canonical" href="https://getbootstrap.com/docs/4.5/examples/sign-in/">
    <script src="http://code.jquery.com/jquery-latest.js"></script>

    <!-- Bootstrap core CSS -->
    <link href="<%=request.getContextPath()%>/static/style/assets/dist/css/bootstrap.css" rel="stylesheet">
    <script src="https://cdn.bootcss.com/jquery/2.1.1/jquery.min.js"></script>

    <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
    <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <script type="text/javascript">
        /*  该function用于用户登陆的验证，其功能如下：
            1.当用户账号为空时，警告：用户名必填！
            2.当用户密码为空时，警告：密码必填！
            3.通过ajax连接后端，如果账号密码验证成功，就跳转至主页，否则警告：账号或者密码错误！
        */
        function confirmPwd() {
            var password = $("#password").val();
            var re_password = $("#re_password").val();
            if(password != re_password){
                alert("密码不一致");
                $("#re_password").focus();
                return ;
            }

        }
        function confirmPhone() {
            var phone = $("#phone").val();
            if(phone.length!=11){
                alert("电话格式不正确");
                $("#phone").focus();
                return ;
            }

        }
        function confirmEmail() {
            var username = $("#username").val();
            var  reg=/^[a-zA-Z\d]+([-_.][A-Za-z\d]+)*@[a-zA-Z0-9]{1,6}.([c,o,m]{3})|([c,n]{2})$/
            if(!reg.test(username)){
                alert("邮箱格式错误");
                $("#username").focus();//获取焦点
                return ;
            }
        }
        function register()
        {
            var username = $("#username").val();//取值
            var password = $("#password").val();
            var nickname = $("#nickname").val();
            var re_password = $("#re_password").val();
            var phone = $("#phone").val();

            if(!username){
                alert("用户名必填!");
                $("#username").focus();//获取焦点
                return ;
            }
            if(!nickname){
                alert("昵称必填!");
                $("#nickname").focus();//获取焦点
                return ;
            }
            if(!password){
                alert("密码必填!");
                $("#password").focus();//获取焦点
                return ;
            }
            if(!re_password){
                alert("确认密码必填!");
                $("#re_password").focus();//获取焦点
                return ;
            }
            if(!phone){
                alert("电话必填!");
                $("#phone").focus();//获取焦点
                return ;
            }
            var  reg=/^[a-zA-Z\d]+([-_.][A-Za-z\d]+)*@[a-zA-Z0-9]{1,6}.([c,o,m]{3})|([c,n]{2})$/
            if(!reg.test(username)){
                alert("邮箱格式错误");
                $("#username").focus();//获取焦点
                return ;
            }
            if(password != re_password){
                alert("密码不一致");
                $("#re_password").focus();
                return ;
            }
            if(phone.length!=11){
                alert("电话格式不正确");
                $("#phone").focus();
                return ;
            }


            $.ajax({
                //几个参数需要注意一下
                type: "post",//方法类型
                dataType: "json",//预期服务器返回的数据类型
                url: "/user/doregister" ,//url
                async:false,
                data: {
                    "username":username,
                    "password":password,//多数据用,分隔
                    "phone":phone,
                    "nickname":nickname
                },
                success:
                    function (data) {
                        if (data.msg == "ok") {
                            alert("注册成功!");
                            window.location.href="/user/login";
                        }
                        else if(data.msg =="badUsername" ){
                            alert("请输入正确的邮箱地址!");
                        }
                        else if(data.msg == "hasUsername"){
                            alert("邮箱地址已注册");
                        }
                        else
                        {
                            alert("注册发生失败!");
                        }
                        ;
                    },
                error :
                    function() {
                        alert("不好意思，服务器跑到火星去了，注册异常！");
                    }
            });
        }
    </script>

    <style>
        .bd-placeholder-img {
            font-size: 1.125rem;
            text-anchor: middle;
            -webkit-user-select: none;
            -moz-user-select: none;
            -ms-user-select: none;
            user-select: none;
        }

        @media (min-width: 768px) {
            .bd-placeholder-img-lg {
                font-size: 3.5rem;
            }
        }
    </style>
    <!-- Custom styles for this template -->
    <link href="<%=request.getContextPath()%>/static/style/assets/dist/css/signin.css" rel="stylesheet">
</head>
<body class="text-center">
<form class="form-signin" >
    <img class="mb-4" src="<%=request.getContextPath()%>/static/style/assets/brand/bootstrap-solid.svg" alt="" width="72" height="72">
    <h1 class="h3 mb-3 font-weight-normal">注册</h1>
    <label for="username" class="sr-only">邮箱地址</label>
    <input type="email" id="username" name="username" class="form-control" placeholder="邮箱地址" onchange="confirmEmail()" required autofocus>
    <label for="nickname" class="sr-only">昵称</label>
    <input type="text" id="nickname" name="nickname" class="form-control" placeholder="昵称" required>
    <label for="password" class="sr-only">密码</label>
    <input type="password" id="password" name="password" class="form-control" placeholder="密码" required>
    <label for="re_password" class="sr-only">确认密码</label>
    <input type="password" id="re_password" name="re_password" class="form-control" placeholder="确认密码" onchange="confirmPwd()" required>
    <label for="phone" class="sr-only">电话</label>
    <input type="text" id="phone" name="phone" class="form-control" placeholder="电话" onchange="confirmPhone()" required>
    <div class="checkbox mb-3">
        <label>
            已有账号?&nbsp;<a href="/user/login">点此登录</a>
        </label>
    </div>
    <button class="btn btn-lg btn-primary btn-block" type="button" onclick="register()">注册</button>
    <p class="mt-5 mb-3 text-muted">&copy; 2021-07</p>
</form>


</body>
</html>